package com.spdg.ring.xml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.wolf.tools.Log;

import com.spdg.ring.entity.MenuEntity;
import com.spdg.ring.sqlite.model.Menu;

/**
 * <p>Title: 菜单解析器         </p>
 * <p>Description: 
 *  根据uri地址，获取到相应的xml并且转换为实体类
 *  <?xml version="1.0" encoding="UTF-8"?>
	<level title="" about="http://newapps.appscomb.cn/op/about_comb_cn?pt=turnfuzhou" pageindex="" tapindex="" slogan="" date="2012-10-25 08:16:15">
		<item title="福州绕圈圈" date="2012-10-25 08:16:15" 
		   src="http://newapps.appscomb.cn/op/turnfuzhou_play?pt=turnfuzhou" icon="http://newapps.appscomb.cn/res/images/turnfuzhou/2.png" type="Table" name="玩乐速递" id="turnfuzhou_02"/> 
		<item title="福州绕圈圈" date="2012-10-25 08:16:15" 
		   src="http://newapps.appscomb.cn/op/turnfuzhou_notice?pt=turnfuzhou" icon="http://newapps.appscomb.cn/res/images/turnfuzhou/3.png" type="Table" name="High消息" id="turnfuzhou_03"/> 
		<item title="福州绕圈圈" about="http://newapps.appscomb.cn/op/about_comb_cn?pt=turnfuzhou" date="2012-10-25 08:16:15" 
		   src="http://newapps.appscomb.cn/op/turnfuzhou_gift?pt=turnfuzhou" icon="http://newapps.appscomb.cn/res/images/turnfuzhou/4.png" type="Item" name="伴手好礼" id="turnfuzhou_04" endnode="1" shareenable="" favoenable="" favolistenable="1"/> 
		<item title="福州绕圈圈" date="2012-10-25 08:16:15" 
		   src="http://newapps.appscomb.cn/op/turnfuzhou_go?pt=turnfuzhou" icon="http://newapps.appscomb.cn/res/images/turnfuzhou/5.png" type="Table" name="Going福州" id="turnfuzhou_05"/> 
	    <item title="福州绕圈圈" date="2012-10-25 08:16:15" 
	       src="http://newappshot.appscomb.cn/op/comb_turnfuzhou_app" icon="http://newapps.appscomb.cn/res/images/turnfuzhou/1.png" type="Table" name="好APP" id="turnfuzhou_01"/> 
	 </level>
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-25             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class MenuParser {

	public MenuEntity parse(InputStream stream) {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();

			Document document = builder.parse(stream);

			Element root = document.getDocumentElement();

			return traverse(root);

		} catch (Exception ex) {
			ex.printStackTrace();
			Log.e("解析xml异常，异常为：" + ex);
		}

		return null;
	}

	protected MenuEntity traverse(Element root) {
		List<Menu> list = new ArrayList<Menu>();
		MenuEntity entity = new MenuEntity();

		if (root == null) {
			return entity;
		}

		if (root.hasAttribute("about")) {
			String about = root.getAttribute("about");
			entity.setAbout(about);
		}

		if (root.hasAttribute("title")) {
			String title = root.getAttribute("title");
			entity.setTitle(title);
		}

		NodeList nodelist = root.getChildNodes();
		int length = nodelist.getLength();
		for (int i = 0; i < length; i++) {
			Node node = nodelist.item(i);

			Menu menu = new Menu();

			NamedNodeMap nodes = node.getAttributes();

			if (nodes != null) {
				Node srcNode = nodes.getNamedItem("src");
				if (srcNode != null)
					menu.setSrc(srcNode.getNodeValue());

				Node typeNode = nodes.getNamedItem("type");
				if (typeNode != null)
					menu.setType(typeNode.getNodeValue());

				Node nameNode = nodes.getNamedItem("name");
				if (nameNode != null)
					menu.setName(nameNode.getNodeValue());

				Node iconNode = nodes.getNamedItem("icon");
				if (iconNode != null)
					menu.setIcon(iconNode.getNodeValue());

				Node iconFile = nodes.getNamedItem("icon_file");
				if (iconFile != null)
					menu.setIconFile(iconFile.getNodeValue());

				Node slogan = nodes.getNamedItem("slogan");
				if (slogan != null)
					menu.setSlogan(slogan.getNodeValue());

				Node file = nodes.getNamedItem("file");
				if (file != null)
					menu.setFile(file.getNodeValue());
				
				Node icon2 = nodes.getNamedItem("icon2");
				if (icon2 != null)
					menu.setIcon2(icon2.getNodeValue());
				
				Node icon2File = nodes.getNamedItem("icon2_file");
				if (icon2File != null)
					menu.setIcon2File(icon2File.getNodeValue());

				list.add(menu);
			}
		}

		entity.setMenus(list);

		return entity;
	}

}
